package com.group12uiowaece.bluetoothexerciseapplication;

import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.GraphViewSeries;
import com.jjoe64.graphview.LineGraphView;
import com.jjoe64.graphview.GraphView.GraphViewData;

import android.app.Fragment;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Spinner;

public class GraphFragment extends Fragment {

	private Spinner exerciseSelectMenu;
	private Spinner timespanSelectMenu;
	private Button loadGraphButton;

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View v = inflater.inflate(R.layout.graph_fragment, container, false);

		exerciseSelectMenu = (Spinner) v.findViewById(R.id.exerciseSelect);
		// Create an ArrayAdapter using the string array and a default spinner
		// layout
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
				this.getActivity(), R.array.exercise_types_array,
				android.R.layout.simple_spinner_item);
		// Specify the layout to use when the list of choices appears
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		// Apply the adapter to the spinner
		exerciseSelectMenu.setAdapter(adapter);

		timespanSelectMenu = (Spinner) v.findViewById(R.id.timespanSelect);
		// Create an ArrayAdapter using the string array and a default spinner
		// layout
		adapter = ArrayAdapter.createFromResource(this.getActivity(),
				R.array.timespan_array, android.R.layout.simple_spinner_item);
		// Specify the layout to use when the list of choices appears
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		// Apply the adapter to the spinner
		timespanSelectMenu.setAdapter(adapter);

		loadGraphButton = (Button) v.findViewById(R.id.loadGraphButton);
		loadGraphButton.setOnClickListener(buttonListener);

		return v;

	}

	OnClickListener buttonListener = new OnClickListener() {
		public void onClick(View v) {
			// init example series data
			GraphViewSeries exampleSeries = new GraphViewSeries(
					new GraphViewData[] {
							new GraphViewData(1, 1.0d),
							new GraphViewData(2, 1.5d),
							new GraphViewData(3, 2.5d),
							new GraphViewData(4, 2.0d),
							new GraphViewData(5, 3.0d) });

			// graph with dynamically genereated horizontal and vertical labels
			GraphView graphView;

			graphView = new LineGraphView(getActivity() // context
					, "Demonstration Graph" // heading
			);

			graphView.addSeries(exampleSeries); // data

			LinearLayout layout = (LinearLayout) getView().findViewById(R.id.graph_view);
			if (layout == null) {
				Log.w("LAYOUT", "NULL");
			} else {
				layout.addView(graphView);
			}
		}
	};

}
